import copy
from base import LinkType, XMLBase
from characterization import RepresentationLink, Characterization

class DatasetLink(LinkType):
    _node_name = 'Dataset'
    
class InputDatasetLink(LinkType):
    _node_name = 'InputDataset'

class ObjectCollection(XMLBase):
    _node_name = 'ObjectCollection'
    _attrib_map = {'Count':'count'}
    _attrib_class_map = {'Representation':{'attribute':'representation',
                                           'class':RepresentationLink}}

    def __init__(self, count=0, representation=None):
        
        self.count = count
        self.representation = representation

    def _build_tuple(self):
        self._elements = [('Count',int(self.count))]
        if self.representation:
            self._elements.insert(0, self.representation.to_tuple())

    def read(self, xml_str):
        XMLBase.read(self, xml_str)
        if self.count:
            self.count = int(self.count)

class RunLink(LinkType):
    _node_name = 'Run'

class Dataset(XMLBase):
    _node_name = 'Dataset'
    _attrib_map = {'Name':'name',
                   'Time':'time',
                   'SimulationTime':'simulation_time',
                   'TimeStep':'time_step',
                   'Size':'size'}
    _attrib_class_map = {'Run':{'attribute':'run',
                                'class':RunLink}}
    _list_map = {'Characterization':{'attribute':'characterizations',
                                     'class':Characterization},
                 'ObjectCollection':{'attribute':'object_collections',
                                     'class':ObjectCollection}}

    def __init__(self, name=None, run=None, time=None,
                 simulation_time=None, time_step=None, size=None,
                 object_collections=[], characterizations=[]):

        self.name = name
        self.run = run
        self.time = time
        self.simulation_time = simulation_time
        self.time_step = time_step
        self.size = size
        self.object_collections = copy.copy(object_collections)
        self.characterizations = copy.copy(characterizations)

    def read(self, xml_str):
        XMLBase.read(self, xml_str)
        if self.size:
            self.size = int(self.size)
        if self.time_step:
            self.time_step = int(self.time_step)
        if self.simulation_time:
            self.simulation_time = float(self.simulation_time)
